package com.ruoyi.web.controller.agent;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Withdraw;
import com.ruoyi.system.dto.WithdrawSubmitDto;
import com.ruoyi.system.service.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "商户后台:提现记录")
@RestController
@RequestMapping("agent/money")
public class AgentWithdrawController extends BaseController {
    @Autowired
    private WithdrawService withdrawService;


    @ApiOperation(value = "提现")
    @PostMapping("withdraw")
    public ResponseResult withdraw(@Valid @RequestBody WithdrawSubmitDto withdrawSubmitDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        withdrawService.agentSubmitWithdrawal(user, withdrawSubmitDto.getMoney(), withdrawSubmitDto.getAddress());
        return this.setSuccessResult();
    }

    @ApiOperation(value = "提现列表")
    @GetMapping("withdrawList")
    public ResponseResult<List<Withdraw>> withdrawList(String status) {
        String type = "withdraw";
        List<Withdraw> list = this.list(type, status);
        return this.setSuccessResult(this.getPageResult(list));
    }


    private List<Withdraw> list(String type, String status) {
        Long userId = this.getUserId();
        this.startPage();
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(userId);
        withdraw.setType(type);
        if (StrUtil.isNotBlank(status)) {
            withdraw.setStatus(status);
        }
        List<Withdraw> list = withdrawService.findList(withdraw);
        return list;
    }


}
